package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.dubbo.server.api.SoundApi;
import com.tanhua.dubbo.server.api.UserLikeApi;
import com.tanhua.dubbo.server.pojo.Sound;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;

import com.tanhua.server.vo.SoundVo;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@Slf4j
public class SoundService {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;    //上传视频文件的连接客户端

    @Autowired
    private FdfsWebServer fdfsWebServer;    //获取上传的根路径，根据配置文件获取

    @Autowired
    private PicUploadService picUploadService;

    @Autowired
    private UserInfoService userInfoService;

    @Reference(version = "1.0.0")
    private SoundApi soundApi;


    
    
    public SoundVo receiveSound() {
        User user = UserThreadLocal.get();
        SoundVo soundVo = new SoundVo();
        soundVo.setRemainingTimes(0);

        Sound remain = soundApi.getRemain(user.getId());
        Integer remainListenTime = remain.getRemainListenTimes();

        if (remainListenTime > 0) {
            Sound sound = soundApi.getSound(user.getId());
            UserInfo userInfo = userInfoService.queryById(sound.getUserId());
            //封装AudioVo对象
            soundVo.setId(userInfo.getId());    //发布音频用户id
            soundVo.setAvatar(userInfo.getLogo());  //头信息
            soundVo.setNickname(userInfo.getNickName());    //昵称
            soundVo.setGender(userInfo.getSex().name().toLowerCase());  //性别
            soundVo.setAge(userInfo.getAge());  //年龄
            soundVo.setSoundUrl(sound.getSoundUrl());  //音频地址
            soundVo.setRemainingTimes(remainListenTime - 1);    //剩余次数

            remain.setRemainListenTimes(remainListenTime-1);
            soundApi.updateAudioTime(remain);
        }

        return soundVo;
    }


    /**
     *
     * 保存语音文件
     * @param soundFile
     * @return
     */

    public String saveFile(MultipartFile soundFile) {
        User user = UserThreadLocal.get();

        Sound sound = new Sound();
        try {
            //查询今日剩余次数
            Sound remain = soundApi.getRemain(user.getId());

            if (remain.getRemainSendTimes() > 0) {
                sound.setUserId(user.getId());  //用户id

                if (soundFile != null) {
//                    PicUploadResult picUploadResult = this.picUploadService.upload(soundFile);
//                    sound.setSoundUrl(picUploadResult.getName());   //再阿里云oss中

                    StorePath storePath = fastFileStorageClient.uploadFile(soundFile.getInputStream(),  //获取输入流
                            soundFile.getSize(),    //获取文件大小
                            StringUtils.substringAfter(soundFile.getOriginalFilename(), "."),   //获取后缀
                            null);
                    sound.setSoundUrl(fdfsWebServer.getWebServerUrl() + "/" + storePath.getFullPath()); //设置视频路径
                }

                //更新剩余次数
                remain.setRemainSendTimes(remain.getRemainSendTimes() - 1);
                sound.setRemainSendTimes(remain.getRemainSendTimes());
                sound.setRemainListenTimes(remain.getRemainListenTimes());
                sound.setRefresh(remain.getRefresh());

                System.out.println("用户:" + user.getId() + " 今日剩余保存次数:" + remain.getRemainSendTimes());
                soundApi.updateAudioTime(remain);

                return soundApi.saveSound(sound);
            }
        } catch (IOException e) {
            log.error("保存音频文件出错！",e);
        }
        return null;
    }

    @Reference(version = "1.0.0")
    private UserLikeApi userLikeApi;

    @Autowired
    private IMService imService;

    public Boolean likeUser(Long likeUserId) {
        User user = UserThreadLocal.get();
        String id = this.userLikeApi.saveUserLike(user.getId(), likeUserId);
        if (StringUtils.isEmpty(id)) {
            return false;
        }

        if (this.userLikeApi.isMutualLike(user.getId(), likeUserId)) {
            //相互喜欢成为好友
            this.imService.contactUser(likeUserId);
        }

        return true;

    }
}
